package com.jic.interaction.ChinaPost.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.jic.common.base.vo.RestResult;
import com.jic.interaction.common.enums.OrderInfoEnums;
import com.jic.interaction.common.exception.OrderInfoException;
import com.jic.interaction.common.utils.HttpClientUtil;
import com.jic.interaction.ChinaPost.model.vo.request.OrderInfoRequest;
import com.jic.interaction.ChinaPost.model.vo.request.OrderLogisticsInfoRequest;
import com.jic.interaction.ChinaPost.service.OrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 * 订单信息同步接口实现类
 * </p>
 *
 * @author syf
 * @since 2020-11-05
 */
@Service
@Slf4j
public class OrderInfoServiceImpl implements OrderInfoService {


    @Value("${route.value}")
    private String url;



    /**
     * @Author: songyafei
     * @Package: com.jic.interaction.service.impl
     * @ClassName: OrderInfoServiceImpl
     * @Description: 同步订单信息接口实现
     * @Param [orderInfoRequest]
     * @Exception
     * @CreateDate: 2020/11/5 15:27
     * @UpdateRemark: The modified content
     * @Version: 1.0
     * @Return :        com.jic.common.base.vo.RestResult<?>
     **/

    @Override
    public RestResult<?> orderInfosync(OrderInfoRequest orderInfoRequest) throws InterruptedException, ExecutionException, IOException {
        log.info("同步订单信息开始：请求参数{1}", JSONObject.toJSONString(orderInfoRequest));
        String url=this.url+"/m/dbs-front/order/syncOrder";
        this.dataVerif(orderInfoRequest, null);
        Map<String,String> parem =JSON.parseObject(JSON.toJSONString(orderInfoRequest), Map.class);
        log.info("同步订单信息结束");
        String body =  HttpClientUtil.doRequestPost(url, parem, null);
        return RestResult.success(this.getReturnData(body));
    }

    /**
     * @Author:         songyafei
     * @Package:        com.jic.interaction.service.impl
     * @ClassName:      OrderInfoServiceImpl
     * @Description:     同步订单物流信息
     * @Param           [orderInfoRequest]
     * @Exception
     * @CreateDate:     2020/11/5 16:50
     * @UpdateRemark:   The modified content
     * @Version:        1.0
     * @Return :        com.jic.common.base.vo.RestResult<?>
     **/

    @Override
    public RestResult<?> logisticsSync(OrderLogisticsInfoRequest orderLogisticsInfoRequest) throws InterruptedException, ExecutionException, IOException {
        log.info("同步订单物流信息开始：请求参数{}", orderLogisticsInfoRequest.toString());
        String url=this.url+"/m/dbs-front/order/logisticsSynchronize";
        this.dataVerif(orderLogisticsInfoRequest, null);
        Map<String,String> parem =JSON.parseObject(JSON.toJSONString(orderLogisticsInfoRequest), Map.class);
        String body =  HttpClientUtil.doRequestPost(url, parem, null);
        log.info("同步订单物流信息结束");
        return RestResult.success(this.getReturnData(body));
    }

    /**
     * @Author: songyafei
     * @Package: com.jic.interaction.service.impl
     * @ClassName: OrderInfoServiceImpl
     * @Description: 同步订单数据非空校验方法，orderInfoRequest为数据 ，code 异常码，args为异常提示信息
     * @Param [orderInfoRequest, code, args]
     * @Exception
     * @CreateDate: 2020/11/5 15:34
     * @UpdateRemark: The modified content
     * @Version: 1.0
     * @Return :        void
     **/
//1
    private void dataVerif(OrderInfoRequest orderInfoRequest, String code, String... args) {
        if(StringUtils.isEmpty(orderInfoRequest.getAppId())) {
            throw new OrderInfoException(OrderInfoEnums.ORDER_INFO_APPID_IS_NULL);
        }
        if(StringUtils.isEmpty(orderInfoRequest.getUserId())) {
            throw new OrderInfoException(OrderInfoEnums.ORDER_INFO_USERID_IS_NULL);
        }
        if(StringUtils.isEmpty(orderInfoRequest.getOrderNo())) {
            throw new OrderInfoException(OrderInfoEnums.ORDER_INFO_ORDER_NO_IS_NULL);
        }
        if(StringUtils.isEmpty(orderInfoRequest.getOrderTime())) {
            throw new OrderInfoException(OrderInfoEnums.ORDER_INFO_ORDER_TIME_IS_NULL);
        }
        if(StringUtils.isEmpty(orderInfoRequest.getEstimatedArrivalTime())) {
            throw new OrderInfoException(OrderInfoEnums.ORDER_INFO_ORDER_TIME_IS_NULL);
        }

    }
    /**
     * @Author: songyafei
     * @Package: com.jic.interaction.service.impl
     * @ClassName: OrderInfoServiceImpl
     * @Description: 同步订单物流数据非空校验方法，orderInfoRequest为数据 ，code 异常码，args为异常提示信息
     * @Param [orderInfoRequest, code, args]
     * @Exception
     * @CreateDate: 2020/11/5 15:34
     * @UpdateRemark: The modified content
     * @Version: 1.0
     * @Return :        void
     **/

    private void dataVerif(OrderLogisticsInfoRequest orderLogisticsInfoRequest, String code, String... args) {
        if(StringUtils.isEmpty(orderLogisticsInfoRequest.getAppId())) {
            throw new OrderInfoException(OrderInfoEnums.ORDER_INFO_APPID_IS_NULL);
        }
        if(StringUtils.isEmpty(orderLogisticsInfoRequest.getUserId())) {
            throw new OrderInfoException(OrderInfoEnums.ORDER_INFO_USERID_IS_NULL);
        }
        ;
        if(StringUtils.isEmpty(orderLogisticsInfoRequest.getOrderNo())) {
            throw new OrderInfoException(OrderInfoEnums.ORDER_INFO_ORDER_NO_IS_NULL);
        }
        if(StringUtils.isEmpty(orderLogisticsInfoRequest.getActualArrivalTime())) {
            throw new OrderInfoException(OrderInfoEnums.ORDER_INFO_ESTIMATEARRIVAL_TIME_IS_NULL);
        }

    }

    /**
     * @Author:         songyafei
     * @Package:        com.jic.interaction.service.impl
     * @ClassName:      OrderInfoServiceImpl
     * @Description:     解析结果方法
     * @Param           [body]
     * @Exception
     * @CreateDate:     2020/11/5 16:57
     * @UpdateRemark:   The modified content
     * @Version:        1.0
     * @Return :        java.lang.String
     **/

    private String getReturnData(String body) {
        Map<String, Object> returnMap = Maps.newHashMap();
        //if (org.springframework.util.StringUtils.isEmpty(body)) throw new Exception("返回结果为空");
        JSONObject jsonObject = JSON.parseObject(body);
        String code = jsonObject.getString("code");
        // code不是0是表示获取失败
        return jsonObject.getString("msg");

    }

}
